package com.syj.jsxjj.ui.my.fragment;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.syj.jsxjj.R;
import com.syj.jsxjj.base.BaseFragment;
import com.syj.jsxjj.ui.main.MainActivity;
import com.syj.jsxjj.ui.my.adapter.CouponThreeAdapter;
import com.syj.jsxjj.ui.my.bean.CouponBean;
import com.syj.jsxjj.ui.my.contract.CouponContract;
import com.syj.jsxjj.ui.my.presenter.CouponPresenter;
import com.syj.jsxjj.widget.refresh.base.OnLoadMoreListener;
import com.syj.jsxjj.widget.refresh.base.OnRefreshListener;
import com.syj.jsxjj.widget.refresh.base.SwipeToLoadLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by gyl on 2017/5/5 0005.
 */

public class CouponThreeFragment extends BaseFragment<CouponPresenter> implements CouponContract.View, OnRefreshListener, OnLoadMoreListener {
    @BindView(R.id.tv_coupon)
    TextView mTvCoupon;
    @BindView(R.id.tv_cantUse)
    TextView mTvCantUse;
    @BindView(R.id.swipe_target)
    RecyclerView mRefreshList;
    @BindView(R.id.refresh)
    SwipeToLoadLayout mRefreshLoadLayout;
    @BindView(R.id.ll_nodata)
    LinearLayout mLlNodata;

    private int pageNum = 1;
    private CouponThreeAdapter mAdapter;
    private List<CouponBean.ItemBean> itemBeanList;
    private boolean isPull = true;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_coupon;
    }

    @Override
    public void initPresenter() {
        mPresenter.init(this);
        mPresenter.Coupon("-1", pageNum + "");
    }

    @Override
    public void loadData() {
        initView();
    }

    private void initView() {
        mRefreshLoadLayout.setOnRefreshListener(this);
        mRefreshLoadLayout.setOnLoadMoreListener(this);
        mRefreshList.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new CouponThreeAdapter();
        mRefreshList.setAdapter(mAdapter);
    }


    @OnClick(R.id.tv_cantUse)
    public void onClick() {
        Intent intent = new Intent(mActivity, MainActivity.class);
        mActivity.setResult(1024, intent);
        mActivity.finish();
    }

    @Override
    public void onRefresh() {
        pageNum = 1;
        isPull = true;
        mPresenter.Coupon("-1", pageNum + "");
    }

    @Override
    public void onLoadMore() {
        if (itemBeanList != null) {
            pageNum++;
            isPull = false;
            mPresenter.Coupon("-1", pageNum + "");
            onComplete(mRefreshLoadLayout);
        } else {
            mRefreshLoadLayout.setLoadingMore(false);
            mRefreshLoadLayout.setLoadingMore(false);
            onComplete(mRefreshLoadLayout);
        }
    }

    @Override
    public void showLoading(String content) {

    }

    @Override
    public void stopLoading() {
        onComplete(mRefreshLoadLayout);
    }

    @Override
    public void showErrorMsg(String msg, String type) {

    }

    @Override
    public void CouponSuccess(List<CouponBean.ItemBean> item) {
        if (item != null) {
            if (isPull) {
                mAdapter.clearData();
                onComplete(mRefreshLoadLayout);
                if (item == null || item.size() == 0) {
                    if (mAdapter.getFootersCount() == 0) {
                        mLlNodata.setVisibility(View.VISIBLE);
                        mTvCoupon.setText("暂无已过期的优惠券哦~~");
                    }
                } else {
                    if (mAdapter.getFootersCount() > 0)
                        mAdapter.clearFooterView();
                }
            }
            itemBeanList = item;
            mAdapter.addData(item);
        } else {
            mAdapter.clearData();
            if (mAdapter.getFootersCount() == 0) {
                mLlNodata.setVisibility(View.VISIBLE);
                mTvCoupon.setText("暂无已使用的优惠券哦~~");
            }
        }
    }
}
